package com.joysuch.wwyt.common.controller;


import com.joysuch.wwyt.common.service.ExcelTableService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zqw
 * @since 2021-05-31
 */
@Slf4j
@RestController
@RequestMapping("data/template")
@Api(tags = "权限数据导入导出")
public class ExcelTableController {

    @Autowired
    private ExcelTableService excelTableService;


//    @PostMapping("saveTable")
//    @ApiOperation(value = "保存excel表名配置", response = ExcelTable.class)
//    public ResultBean saveTable(@RequestBody @Valid ExcelTableBean bean) {
//        return excelTableService.saveTable(bean);
//    }
//
//    @PostMapping("pageTable")
//    @ApiOperation(value = "分页excel表名配置")
//    public ResultBean pageTable(ExcelTableSearchBean searchBean) {
//        return excelTableService.pageTable(searchBean);
//    }


//    @GetMapping("deleteTable")
//    @ApiOperation(value = "删除excel表名配置")
//    public ResultBean deleteTable(Long id) {
//        return excelTableService.deleteTable(id);
//    }
//
//    @PostMapping("saveField")
//    @ApiOperation(value = "保存excel字段配置", response = ExcelField.class)
//    public ResultBean saveTable(@RequestBody @Valid ExcelFieldBean bean) {
//        return excelTableService.saveField(bean);
//    }
//
//    @GetMapping("listField")
//    @ApiOperation(value = "excel字段配置列表展示")
//    public ResultBean listField(Long tableId) {
//        return excelTableService.listField(tableId);
//    }
//
//    @GetMapping("deleteField")
//    @ApiOperation(value = "删除excel字段配置")
//    public ResultBean deleteField(Long id) {
//        return excelTableService.deleteField(id);
//    }


    @GetMapping("listTable")
    @ApiOperation(value = "excel表名配置列表展示")
    public ResultBean listTable() {
        return excelTableService.listTable();
    }

    @GetMapping("exportExcelData")
    @ApiOperation(value = "导出")
    @RequiresPermissions("data:template")
    public ResultBean exportExcelData(HttpServletResponse response, @RequestParam(value = "idList") List<Long> idList) {
        try {
            SXSSFWorkbook workBook = excelTableService.exportExcelData(idList);
            ServletOutputStream outputStream = response.getOutputStream();
            String name = "数据权限导入模板.xlsx";
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(name, "UTF-8"));
            workBook.write(outputStream);
            outputStream.close();
            return ResultBean.defaultSuccessResult();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "下载失败");
        }
    }

    @PostMapping("importExcelData")
    @ApiOperation("导入")
    @RequiresPermissions("data:template")
    public ResultBean importDistrictEvaluationExcel(MultipartFile file) {
        List<String> meaages = null;
        try {
            meaages = excelTableService.importExcelData(file);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "导入失败");
        }
        return ResultBean.success(meaages);
    }

}

